package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Workflow;
import com.qidianit.mes.pro.mapper.WorkflowMapper;
import com.qidianit.mes.pro.service.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ Description:
 * @ Author:WuJingLei
 * @ Date: 2025/9/18 -14:14
 */
@Service
public class WorkflowServiceImpl implements WorkflowService {
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Result<PageInfo<Workflow>> queryWorkflows(Workflow workflow,int page,int size) {
        //分页
        PageHelper.startPage(page,size);
        //查询数据
        List<Workflow> workflows = workflowMapper.queryWorkflow(workflow);

        return Result.success(new PageInfo<>(workflows));
    }

    @Override
    public Result<Workflow> queryByCode(String code) {
        return Result.success(workflowMapper.queryByCode(code));
    }

    @Override
    public Result saveWorkflow(Workflow workflow) {
        Workflow workflow1 = workflowMapper.queryByCode(workflow.getWorkflowCode());
        if (workflow1 !=null){
            return Result.error(500,"编码已存在",null);
        }
        return workflowMapper.saveWorkflow(workflow)>0?Result.success(workflow):Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workflowMapper.deleteById(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] id) {
        return workflowMapper.batchDelete(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result updateWorkflow(Workflow workflow) {
        Workflow workflow1 = workflowMapper.queryByCode(workflow.getWorkflowCode());
        if (workflow1 !=null && !Objects.equals(workflow1.getId(), workflow.getId())){
            return Result.error(500,"编码已存在",null);
        }
        return workflowMapper.updateWorkflow(workflow)>0?Result.success():Result.error("操作失败");
    }
}
